<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Pre;
use Input;
use Session;

class AdorderController extends Controller
{
    //

    public function getaddpre(){
      return view('addpre');
    }
    public function postaddpre(Request $req){
      $pre=new \App\Pre();
      if(!empty($_POST)){
        $pre->pre_name=$_POST['pre_name'];
        $pre->jifen=$_POST['jifen'];
        $pre->desc=$_POST['desc'];

        // $file=Input::file('img');
        $file=$req->file('img');
        if($file->isValid()){
        //   $clientName=$file->getClientOriginalName();
          $entension = $file -> getClientOriginalExtension(); //上传文件的后缀
        //    $mimeTye = $file -> getMimeType();//得到后缀类型
          $newName = rand(1000,9999).".".$entension;
           $path = $file -> move('storage/uploads',$newName);
           $pre->img=$path;
         }
         if($pre->save()){
           return redirect('prelist');
         }else{
           return '上传失败';
         }

        //return $pre->save() ? 'ok' : 'fail';
      }

    }
    public function present(){
      if(Session::get('name')!=1){
        return redirect('admin');
      }
      $pre=Pre::paginate(10);
      return view('present',['pre'=>$pre]);
    }
    public function predel($id){
      $shan=Pre::find($id);
      return $shan->delete() ? redirect('prelist') : 'fail';
    }
    public function preedit($id){
      if(empty($_POST)){
        $pre=Pre::find($id);
        return view('preedit',['pre'=>$pre]);}
    }
    public function postpreedit($id){
      if(empty($_POST)){
        $pre=Pre::find($id);
        return view('preedit',['pre'=>$pre]);
      }else{
        $pre=Pre::find($id);
        $pre->pre_name=$_POST['pre_name'];
        $pre->jifen=$_POST['jifen'];
        $pre->desc=$_POST['desc'];
        return $pre->save() ? redirect('prelist') : 'fail';
      }
    }

    public function getadmin(){
      return view('denglu');
    }
    public function postadmin(){
      $adminName='admin';
      $adminPwd='123123';
      if(($adminName==$_POST['name'])&&($adminPwd==$_POST['password'])){
        Session::set('name',1);
        return redirect('prelist');
      }else{
        return '密码或者用户名错误';
      }
    }

    public function houtai(){
      return view('houtai');
    }
}
